package cn.edu.nju.software.cdiy.dao.impl;

import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.Horder;
import cn.edu.nju.software.cdiy.bean.Supplier;
import cn.edu.nju.software.cdiy.bean.Value;
import cn.edu.nju.software.cdiy.dao.HorderDao;
import cn.edu.nju.software.cdiy.dao.ValueDao;
import cn.edu.nju.software.cdiy.util.BusinessConstant;

public class HorderDaoImpl implements HorderDao {

	@Autowired
	private SessionFactory sessionFactory;
	@Autowired
	private ValueDao valueDao;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void setValueDao(ValueDao valueDao) {
		this.valueDao = valueDao;
	}

	@Override
	public Horder save(Horder order) {
		Session session = sessionFactory.getCurrentSession();
		

		try {
			
			session.save(order);

			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return order;

	}

	@Override
	public List loadBySupplierId(Long supplier) {
		Session session = sessionFactory.getCurrentSession();

		List list = null;
		try {

			Query query = session
					.createQuery("from Horder h where h.supplier=:supplierid");
			query.setParameter(0, supplier);
			list = query.list();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return list;
	}

	@Override
	public Map<Integer, Horder> createHorderList(GregorianCalendar startDate,
			Session session) {
		Map<Integer, Horder> horderList = new HashMap<Integer, Horder>();

		try {
			// 获取supplier 列表，生成在某个startDate内的硬件订单
			String sql = "from Supplier";
			Query query = session.createQuery(sql);
			List<Supplier> supplierList = query.list();
			Value state = valueDao.getValue(BusinessConstant.HORDER_STATE1);
			Iterator<Supplier> iter = supplierList.iterator();
			Supplier s = null;
			Horder h;
			while (iter.hasNext()) {
				s = iter.next();
				h = new Horder();
				h.setSupplier(s);
				h.setTime(startDate.getTime());
				h.setState(state);

				horderList.put(h.getSupplier().getId(), h);
			}

		} catch (Exception e) {

			e.printStackTrace();
		}
		return horderList;
	}

	// public static final String HORDER_TYPE1="待发货";
	// public static final String HORDER_TYPE2="已发货";
	// public static final String HORDER_TYPE3="成功";
	// public static final String HORDER_TYPE4="失败";
	@Override
	public List<Horder> loadSupplierHorder(int supplier, Value state, int page,
			int pageSize) {
		Session session = sessionFactory.getCurrentSession();

		List<Horder> list = null;
		try {

			Supplier s=(Supplier) session.load(Supplier.class, supplier);
			Query query = session
					.createQuery("from Horder h where h.supplier=:supplierid and h.state=:state order by h.time desc");
			query.setParameter("supplierid", s);
			query.setParameter("state", state);
			query.setFirstResult((page - 1) * pageSize);
			query.setMaxResults(pageSize);
			list = query.list();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return list;
	}

	@Override
	public long getSupplierHorderLength(int supplier, Value state) {
		Session session = sessionFactory.getCurrentSession();

		long  returnInt = 0;
		try {
			Supplier s=(Supplier) session.load(Supplier.class, supplier);
			Query query = session
					.createQuery("select count (*)from Horder h where h.supplier=:supplierid and h.state=:state order by h.time desc");
			query.setParameter("supplierid", s);
			query.setParameter("state", state);
			
			returnInt = (Long) query.uniqueResult();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return returnInt;
	}

	@Override
	public Horder updateHorderState(long horderId, Value state) {
		Session session = sessionFactory.getCurrentSession();

		Horder  returnInt = null;
		try {
			returnInt=(Horder) session.load(Horder.class, horderId);
			returnInt.setState(state);
			session.save(returnInt);

			
			
		

		} catch (Exception e) {

			e.printStackTrace();
		}

		return returnInt;
	}

	@Override
	public List<Horder> getSuplierHorderAll(int supplier, int page,
			int pageSize) {
		Session session = sessionFactory.getCurrentSession();

		List<Horder> list = null;
		try {
			Supplier s=(Supplier) session.load(Supplier.class, supplier);
			Query query = session
					.createQuery("from Horder h where h.supplier=:supplierid and h.state=:state order by h.time desc");
			query.setParameter("supplierid", s);
			
			query.setFirstResult((page - 1) * pageSize);
			query.setMaxResults(pageSize);
			list = query.list();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return list;
	}

	@Override
	public long getSuplierHorderAllLength(int supplier) {
		Session session = sessionFactory.getCurrentSession();

		long  returnInt = 0;
		try {
			Supplier s=(Supplier) session.load(Supplier.class, supplier);
			Query query = session
					.createQuery("select count (*)from Horder h where h.supplier=:supplierid and h.state=:state order by h.time desc");
			query.setParameter("supplierid", s);
			
			
			returnInt = (Long) query.uniqueResult();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return returnInt;
	}

}
